﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Data;
using System.Data.Odbc;

namespace Components.Data
{
    public class Character
    {
        private string pName;
        private string pCharacterId;
        private int pLevel;
        private int pJob;
        private int pSta;
        private int pStr;
        private int pInt;
        private int pDex;
        private Inventory pInventory;

        public string Name
        {
            get { return pName; }
        }

        public string CharacterId
        {
            get { return pCharacterId; }
        }

        public int Level
        {
            get { return pLevel; }
        }

        public int Job
        {
            get { return pJob; }
        }

        public int STA
        {
            get { return pSta; }
        }

        public int STR
        {
            get { return pStr; }
        }

        public int INT
        {
            get { return pInt; }
        }

        public int DEX
        {
            get { return pDex; }
        }

        public Inventory Inventory
        {
            get { return pInventory; }
        }

        public Character(string name, string characterid, int level, int job, int sta, int str, int intel, int dex)
        {
            pName = name;
            pCharacterId = characterid;
            pLevel = level;
            pJob = job;
            pSta = sta;
            pStr = str;
            pInt = intel;
            pDex = dex;

            LoadInventory();
        }

        private void LoadInventory()
        {
            pInventory = new Inventory();
            OdbcConnection conn = new OdbcConnection(Settings.CharacterConnection);
            OdbcCommand cmd = new OdbcCommand("SELECT m_Inventory, m_apIndex FROM INVENTORY_TBL WHERE m_idPlayer = ?", conn);

            cmd.Parameters.Add("@idPlayer", OdbcType.Char,7).Value = pCharacterId;

            OdbcDataReader reader = null;
            try
            {
                conn.Open();

                reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    string strIndex = reader.GetString(1);
                    string[] strIndexes = strIndex.Split('/');
                    for (int i = 0; i < strIndexes.Length - 1; ++i)
                    {
                        Inventory.SetIndex(i, Convert.ToInt32(strIndexes[i]));
                    }

                    string strInvent = reader.GetString(0);

                    if (string.IsNullOrEmpty(strInvent))
                        return;

                    string[] strItems = strInvent.Split('/');

                    for (int i = 0; i < strItems.Length - 1; ++i)
                    {
                        if (strItems[i] == "$")
                            continue;
                        Item item = new Item();
                        int index = item.Load(strItems[i]);
                        pInventory.SetAt(index, item);
                    }
                }
            }
            catch (OdbcException e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
